package com.alibaba.nacos.plugin.datasource.impl.kingbase8;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.nacos.plugin.datasource.constants.DefaultConstant.DEFAULT_TENANT_ID;

public class HistoryConfigInfoMapperByKingbase8 extends AbstractKingbase8Mapper implements HistoryConfigInfoMapper {

    @Override
    public MapperResult removeConfigHistory(MapperContext context) {
        String innerSql = getDatabaseDialect().getLimitTopSqlWithMark("SELECT id FROM his_config_info WHERE gmt_modified < ?");
        String sql="DELETE FROM his_config_info WHERE id IN( " + innerSql + ")";
        Timestamp startTime = (Timestamp)context.getWhereParameter(FieldConstant.START_TIME);

        List<Object> paramList = new ArrayList();
        paramList.add(startTime);

        return new MapperResult(sql, paramList);
    }

    @Override
    public MapperResult findConfigHistoryCountByTime(MapperContext context) {
        String sql="SELECT count(*) FROM his_config_info WHERE gmt_modified < ?";

        Timestamp startTime = (Timestamp)context.getWhereParameter(FieldConstant.START_TIME);
        List<Object> paramList = new ArrayList();
        paramList.add(startTime);

        return new MapperResult(sql, paramList);
    }

    @Override
    public MapperResult findDeletedConfig(MapperContext context) {
        String sql="SELECT DISTINCT data_id, group_id, tenant_id FROM his_config_info WHERE op_type = 'D' AND gmt_modified >= ? AND gmt_modified <= ?";
        Timestamp startTime = (Timestamp)context.getWhereParameter(FieldConstant.START_TIME);
        List<Object> paramList = new ArrayList();
        paramList.add(startTime);

        return new MapperResult(sql, paramList);
    }

    @Override
    public MapperResult findConfigHistoryFetchRows(MapperContext context) {
        String sql="SELECT nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified FROM his_config_info "
                + " WHERE data_id = ? AND group_id = ? " + " AND tenant_id = NVL(?,'" + DEFAULT_TENANT_ID + "') " + " ORDER BY nid DESC";



        String dataId = (String)context.getWhereParameter(FieldConstant.DATA_ID);
        String group = (String)context.getWhereParameter(FieldConstant.GROUP_ID);
        String tenantId = (String)context.getWhereParameter(FieldConstant.TENANT_ID);

        List<Object> paramList = new ArrayList();
        paramList.add(dataId);
        paramList.add(group);
        paramList.add(tenantId);

        return new MapperResult(sql, paramList);
    }

    @Override
    public MapperResult pageFindConfigHistoryFetchRows(MapperContext context) {
        String sql="SELECT nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified FROM his_config_info "
                + " WHERE data_id = ? AND group_id = ? " + " AND tenant_id = NVL(?,'" + DEFAULT_TENANT_ID + "') " + " ORDER BY nid DESC";
        String dataId = (String)context.getWhereParameter(FieldConstant.DATA_ID);
        String group = (String)context.getWhereParameter(FieldConstant.GROUP_ID);
        String tenantId = (String)context.getWhereParameter(FieldConstant.TENANT_ID);

        List<Object> paramList = new ArrayList();
        paramList.add(dataId);
        paramList.add(group);
        paramList.add(tenantId);

        int startRow = context.getStartRow();
        int pageSize = context.getPageSize();
        sql=getDatabaseDialect().getLimitPageSqlWithOffset(sql, startRow, pageSize);
        return new MapperResult(sql, paramList);
    }

    @Override
    public MapperResult detailPreviousConfigHistory(MapperContext context) {
        String sql="SELECT nid,data_id,group_id,tenant_id,app_name,content,md5,src_user,src_ip,op_type,gmt_create,gmt_modified "
                + "FROM his_config_info WHERE nid = (SELECT max(nid) FROM his_config_info WHERE id = ?) ";


        long Id=(long)context.getWhereParameter(FieldConstant.ID);

        List<Object> paramList = new ArrayList();
        paramList.add(Id);
        return new MapperResult(sql, paramList);
    }

    @Override
    public String getTableName() {
        return TableConstant.HIS_CONFIG_INFO;
    }
    @Override
    public String getFunction(String s) {
        return s;
    }


}
